//
//  TopListVC.m
//  DayDay
//
//  Created by wade on 16/5/16.
//  Copyright © 2016年 wade. All rights reserved.
//

#import "TopListVC.h"
#import "MJRefresh.h"
#import "PhotoDetailView.h"
#import "AFNetworking.h"
#import "TopListCell.h"
#import "UIImageView+WebCache.h"
@interface TopListVC ()
{
    NSMutableArray *dataArray;
    
    NSInteger pageindex;
}
@end

@implementation TopListVC

static NSString * const reuseIdentifier = @"TopListCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    pageindex = 1;
    
    dataArray = [[NSMutableArray alloc] init];
    
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        [self getdata];
        
    }];
    
    [self.collectionView.mj_footer beginRefreshing];
}
-(void)getdata
{
    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] init];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    //manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    [manager.requestSerializer setTimeoutInterval:30];
    //manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    
    NSMutableDictionary *parameter = [NSMutableDictionary dictionary];
    [parameter setValue:[NSNumber numberWithInteger:pageindex] forKey:@"pageindex"];
    [parameter setValue:[NSNumber numberWithInteger:20] forKey:@"pagesize"];
    
    NSString *strurl = [NSString stringWithFormat:@"https://api.unsplash.com/photos/?client_id=fdfeaab5adfca3f7533e130e152eb240e799daaaf6f785ca7605f514caf9e17c&page=%@&per_page=20",@(pageindex)];
    
    
    [manager GET:strurl parameters:parameter
         progress:^(NSProgress * _Nonnull downloadProgress)
     {
         //NSLog(@"%lld", downloadProgress.totalUnitCount);
     }
          success:^(NSURLSessionDataTask *operation, id responseObject)
     {
         //NSLog(@"%@",responseObject);
         
         [dataArray addObjectsFromArray:responseObject];
         
         if([responseObject count] < 20)
         {
             pageindex = 1;
             [self.collectionView.mj_footer endRefreshingWithNoMoreData];
         }
         else
         {
             pageindex += 1;
             [self.collectionView.mj_footer endRefreshing];
         }
         
         
         [self.collectionView reloadData];
         
     }
          failure:^(NSURLSessionDataTask *operation, NSError *error)
     {
         NSLog(@"%@",error);
         
         [self.collectionView.mj_footer endRefreshing];
     }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [dataArray count];
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    TopListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    NSDictionary *imgDic = [dataArray[indexPath.row] objectForKey:@"urls"];
    NSDictionary *userDic = [dataArray[indexPath.row] objectForKey:@"user"];
    
    [cell.ImgView sd_setImageWithURL:[NSURL URLWithString:[imgDic objectForKey:@"small"]] placeholderImage:nil];
    [cell.likeBtn setTitle:[userDic objectForKey:@"name"] forState:UIControlStateNormal];
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(((self.view.frame.size.width - 40) / 3 ) - 1, (self.view.frame.size.width - 40) / 3);
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    PhotoDetailView *pdv = [storyboard instantiateViewControllerWithIdentifier:@"PhotoDetailView"];
    //pdv.imgList = [dataArray[indexPath.row] objectForKey:@"imglist"];
    
    NSDictionary *imgDic = [dataArray[indexPath.row] objectForKey:@"urls"];
    pdv.imgUrl = [imgDic objectForKey:@"regular"];
    [self.navigationController pushViewController:pdv animated:YES];
}
/*
// Uncomment this method to specify if the specified item should be highlighted during tracking
- (BOOL)collectionView:(UICollectionView *)collectionView shouldHighlightItemAtIndexPath:(NSIndexPath *)indexPath {
	return YES;
}
*/

/*
// Uncomment this method to specify if the specified item should be selected
- (BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
*/

/*
// Uncomment these methods to specify if an action menu should be displayed for the specified item, and react to actions performed on the item
- (BOOL)collectionView:(UICollectionView *)collectionView shouldShowMenuForItemAtIndexPath:(NSIndexPath *)indexPath {
	return NO;
}

- (BOOL)collectionView:(UICollectionView *)collectionView canPerformAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	return NO;
}

- (void)collectionView:(UICollectionView *)collectionView performAction:(SEL)action forItemAtIndexPath:(NSIndexPath *)indexPath withSender:(id)sender {
	
}
*/

@end
